package org.jeecg.modules.basic.worker.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.exitstock.entity.LwgyMaterialExitstock;
import org.jeecg.modules.basic.exitstock.service.ILwgyMaterialExitstockService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.basic.goods.entity.ExportGoods;
import org.jeecg.modules.basic.goods.entity.LwgyMaterialGoods;
import org.jeecg.modules.basic.goods.mapper.LwgyMaterialGoodsMapper;
import org.jeecg.modules.basic.goods.service.ILwgyMaterialGoodsService;
import org.jeecg.modules.basic.material.entity.LwgyInStock;
import org.jeecg.modules.basic.material.service.ILwgyInStockService;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanGoods;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplication;
import org.jeecg.modules.basic.plan.entity.LwgyMaterialPlanapplications;
import org.jeecg.modules.basic.plan.mapper.LwgyMaterialPlanapplicationMapper;
import org.jeecg.modules.basic.plan.service.ILwgyMaterialPlanGoodsService;
import org.jeecg.modules.basic.plan.service.ILwgyMaterialPlanapplicationService;
import org.jeecg.modules.basic.plan.service.ILwgyMaterialPlanapplicationsService;
import org.jeecg.modules.basic.plan.vo.LwgyScoreSanitationPage;
import org.jeecg.modules.basic.storages.entity.ExportStorages;
import org.jeecg.modules.basic.storages.entity.LwgyStoragesNum;
import org.jeecg.modules.basic.storages.service.ILwgyStoragesNumService;
import org.jeecg.modules.basic.worker.entity.LwgyMaterialWorker;
import org.jeecg.modules.basic.worker.entity.LwgyMaterialWorkerUsed;
import org.jeecg.modules.basic.worker.service.ILwgyMaterialWorkerService;
import org.jeecg.modules.basic.worker.service.ILwgyMaterialWorkerUsedService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 物料管理个人管理
 * @Author: jeecg-boot
 * @Date:   2021-04-06
 * @Version: V1.0
 */
@Api(tags="物料管理个人管理")
@RestController
@RequestMapping("/worker/lwgyMaterialWorker")
@Slf4j
public class LwgyMaterialWorkerController extends JeecgController<LwgyMaterialWorker, ILwgyMaterialWorkerService> {
	@Autowired
	private ILwgyMaterialWorkerService lwgyMaterialWorkerService;

	 @Autowired
	 private ILwgyMaterialWorkerUsedService lwgyMaterialWorkerUsedService;
	 @Autowired
	 private ISysBaseAPI sysBaseAPI;


	 /**
	  * 分页列表查询
	  *
	  * @param
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "物料管理个人管理-分页列表查询")
	 @ApiOperation(value="物料管理个人管理-分页列表查询", notes="物料管理个人管理-分页列表查询")
	 @GetMapping(value = "/list")
	 public Result<?> queryPageList(LwgyMaterialWorker lwgyMaterialWorker,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<LwgyMaterialWorker> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialWorker, req.getParameterMap());
		 Page<LwgyMaterialWorker> page = new Page<LwgyMaterialWorker>(pageNo, pageSize);
		 IPage<LwgyMaterialWorker> pageList = lwgyMaterialWorkerService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  * 分页列表查询
	  *
	  * @param
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "物料管理个人管理-分页列表查询")
	 @ApiOperation(value="物料管理个人管理-分页列表查询", notes="物料管理个人管理-分页列表查询")
	 @GetMapping(value = "/list1")
	 public Result<?> list1(LwgyMaterialWorker lwgyMaterialWorker,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {
		 QueryWrapper<LwgyMaterialWorker> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialWorker, req.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		 if (!roles.contains("admin")) {
			 queryWrapper.eq("user_id",sysUser.getId());
		 }
		 Page<LwgyMaterialWorker> page = new Page<LwgyMaterialWorker>(pageNo, pageSize);
		 IPage<LwgyMaterialWorker> pageList = lwgyMaterialWorkerService.queryPageCustomer(page,queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	  * 分页列表查询
	  *
	  * @param
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "物料管理个人管理-分页列表查询")
	 @ApiOperation(value="物料管理个人管理-分页列表查询", notes="物料管理个人管理-分页列表查询")
	 @GetMapping(value = "/list2")
	 public Result<?> list2(LwgyMaterialWorker lwgyMaterialWorker,
							@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
							@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
							@RequestParam(name="id") String id,
							HttpServletRequest req) {
		 QueryWrapper<LwgyMaterialWorker> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialWorker, req.getParameterMap());
		 Page<LwgyMaterialWorker> page = new Page<LwgyMaterialWorker>(pageNo, pageSize);
		 IPage<LwgyMaterialWorker> pageList = lwgyMaterialWorkerService.queryPageCustomers(page,queryWrapper,id);
		 return Result.OK(pageList);
	 }

	 @AutoLog(value = "物料管理个人管理-分页列表查询")
	 @ApiOperation(value="物料管理个人管理-分页列表查询", notes="物料管理个人管理-分页列表查询")
	 @GetMapping(value = "/list3")
	 public Result<?> list3(LwgyMaterialWorker lwgyMaterialWorker,
							@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
							@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
							HttpServletRequest req) {
		 QueryWrapper<LwgyMaterialWorker> queryWrapper = QueryGenerator.initQueryWrapper(lwgyMaterialWorker, req.getParameterMap());
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		 if (!roles.contains("admin")) {
			 queryWrapper.eq("user_id",sysUser.getId());
		 }
		 Page<LwgyMaterialWorker> page = new Page<LwgyMaterialWorker>(pageNo, pageSize);
		 IPage<LwgyMaterialWorker> pageList = lwgyMaterialWorkerService.queryPageGoCustomer(page,queryWrapper);
		 return Result.OK(pageList);
	 }
	 /**
	  *   添加
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "物料管理个人管理-添加")
	 @ApiOperation(value="物料管理个人管理-添加", notes="物料管理个人管理-添加")
	 @PostMapping(value = "/add")
	 public Result<?> add(@RequestBody LwgyMaterialWorker lwgyMaterialWorker) {
		 lwgyMaterialWorkerService.save(lwgyMaterialWorker);
		 return Result.OK("添加成功！");
	 }

	 /**
	  *  编辑
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "物料管理个人管理-编辑")
	 @ApiOperation(value="物料管理个人管理-编辑", notes="物料管理个人管理-编辑")
	 @PutMapping(value = "/edit")
	 public Result<?> edit(@RequestBody  LwgyMaterialWorker lwgyMaterialWorker) {
	 	System.out.println("666666666666666:"+lwgyMaterialWorker);
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 LwgyMaterialWorkerUsed workerUsed=new LwgyMaterialWorkerUsed();
		 workerUsed.setUserId(sysUser.getUsername());
		 workerUsed.setOutDate(new Date());
		 Calendar ca = new GregorianCalendar();// 新建日期对象
		 workerUsed.setYear(String.valueOf(ca.get(Calendar.YEAR)) + "年");
		 workerUsed.setMonth(String.valueOf(ca.get(Calendar.MONTH) + 1) + "月");
		 workerUsed.setDay(String.valueOf(ca.get(Calendar.DAY_OF_MONTH)) + "日");
		BigDecimal a = lwgyMaterialWorker.getApplyNum();
		System.out.println("aaaaaa:"+a);
		BigDecimal b=lwgyMaterialWorker.getOutNum();
		 System.out.println("bbbbb:"+b);
		BigDecimal c = a.subtract(b);
		 System.out.println("ccccc:"+c);
		lwgyMaterialWorker.setApplyNum(c);//0
		 /*BigDecimal zx = lwgyMaterialWorker.getOutNum();
		 BigDecimal d = zx.add(b);*/
		 lwgyMaterialWorker.setOutNum(b);
		 lwgyMaterialWorker.setApplyDate(new Date());
		 lwgyMaterialWorker.setApplyId(sysUser.getUsername());
		 workerUsed.setCampusId(lwgyMaterialWorker.getCampusId());
		 workerUsed.setBuildingId(lwgyMaterialWorker.getBuildingId());
		 workerUsed.setUnitId(lwgyMaterialWorker.getUnitId());
		 workerUsed.setLayerId(lwgyMaterialWorker.getLayerId());
		 workerUsed.setRoomId(lwgyMaterialWorker.getRoomId());
		 workerUsed.setExplains(lwgyMaterialWorker.getExplains());
		 workerUsed.setTypeId(lwgyMaterialWorker.getId());
		 workerUsed.setOutNum(lwgyMaterialWorker.getOutNum());
		 BigDecimal gprice=lwgyMaterialWorker.getPrice();
		 workerUsed.setCountPrice(b.multiply(gprice));
		 lwgyMaterialWorkerService.updateById(lwgyMaterialWorker);
		 lwgyMaterialWorkerUsedService.save(workerUsed);
		 return Result.OK("编辑成功!");
	 }

	 /**
	  *   通过id删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "物料管理个人管理-通过id删除")
	 @ApiOperation(value="物料管理个人管理-通过id删除", notes="物料管理个人管理-通过id删除")
	 @DeleteMapping(value = "/delete")
	 public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		 lwgyMaterialWorkerService.removeById(id);
		 return Result.OK("删除成功!");
	 }

	 /**
	  *  批量删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "物料管理个人管理-批量删除")
	 @ApiOperation(value="物料管理个人管理-批量删除", notes="物料管理个人管理-批量删除")
	 @DeleteMapping(value = "/deleteBatch")
	 public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		 this.lwgyMaterialWorkerService.removeByIds(Arrays.asList(ids.split(",")));
		 return Result.OK("批量删除成功!");
	 }

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "物料管理个人管理-通过id查询")
	 @ApiOperation(value="物料管理个人管理-通过id查询", notes="物料管理个人管理-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyMaterialWorker lwgyMaterialWorker = lwgyMaterialWorkerService.getById(id);
		 if(lwgyMaterialWorker==null) {
			 return Result.error("未找到对应数据");
		 }
		 return Result.OK(lwgyMaterialWorker);
	 }

	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param
	  */
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request,  LwgyMaterialWorker lwgyMaterialWorker) {
		 return super.exportXls(request, lwgyMaterialWorker, LwgyMaterialWorker.class, "物料物品管理");
	 }



	 /**
	  * 通过excel导入数据
	  *
	  * @param request
	  * @param response
	  * @return
	  */
	 @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	 public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		 return super.importExcel(request, response,LwgyMaterialWorker.class);
	 }









}
